﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace InterPC.SPF {

    internal static class Extensions {

        #region IPAddress

        public static bool IsIP4Mapped(this IPAddress ip) {
            if (ip == null || ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
                return false;
            byte[] IP_Bytes = ip.GetAddressBytes();
            return
                IP_Bytes[0] == 0 &&
                IP_Bytes[1] == 0 &&
                IP_Bytes[2] == 0 &&
                IP_Bytes[3] == 0 &&
                IP_Bytes[4] == 0 &&
                IP_Bytes[5] == 0 &&
                IP_Bytes[6] == 0 &&
                IP_Bytes[7] == 0 &&
                IP_Bytes[8] == 0 &&
                IP_Bytes[9] == 0 &&
                IP_Bytes[10] == 0xFF &&
                IP_Bytes[11] == 0xFF;
        }

        public static IPAddress UnmapIP4(this IPAddress ip) {
            if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                return ip;
            byte[] IP4_bytes = ip.GetAddressBytes().Skip(12).ToArray();
            return new IPAddress(IP4_bytes);
        }

	    #endregion

        

    }

}
